<?php

class UserModel extends SCModel {
	
	public function __construct() {
		parent::__construct();
	}
	
	/**
	 * 
	 * 检查用户是否登录
	 * @return ID 或 false
	 */
	public function checkLogin() {
		$identify = SCCookie::get('PASSPORT');
		if($identify && SCIdentity::checkValid($identify)) {
			return SCIdentity::getID($identify);
		} else {
			return false;
		}
	}
	
	/**
	 * 
	 * 检查账号是否正确
	 * 正确则返回用户ID
	 * @return false or ID
	 */
	public function checkAcount($email, $password) {
		$user = $this->from('sc_user')->where('email = "'.$email.'"')->findOne();
		if(!$user) return false;
		if($this->encryptPassword($password) != $user->password) {
			return false;
		}
		return $user->user_id;
	}
	
	/**
	 * 
	 * 设置用户为登录状态
	 */
	public function setLogin($user_id) {
		SCCookie::set('PASSPORT', SCIdentity::setID($user_id), 3600*24);
	}
	
	/**
	 * 
	 * 设置用户为退出状态
	 */
	public function setLogout() {
		SCCookie::delete('PASSPORT');
		SCCookie::clear();
		return true;
	}
	
	/**
	 * 
	 * 获取用户基本信息
	 * @param integer $user_id
	 * @return object
	 */
	public function getBasicInfo($user_id) {
		$user = $this->from('sc_user')->where('user_id = '.$user_id)->findOne();
		unset($user->password);
		return $user;
	}
	
	/**
	 * 加密密码
	 * @param string 用户原始密码
	 * @return string 加密后的密码
	 */
	public function encryptPassword($password) {
		return md5(base64_encode($password));
	}
} 